# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=ruby-concurrent-ruby
_gemname=${pkgname#ruby-}
pkgver=1.3.4
pkgrel=0
pkgdesc="Modern concurrency tools for Ruby"
url="http://www.concurrent-ruby.com"
arch="all"
license="MIT"
depends="ruby"
makedepends="ruby-dev ruby-rake ruby-rake-compiler"
checkdepends="ruby-rspec ruby-timecop"
subpackages="$pkgname-ext"
source="$pkgname-$pkgver.tar.gz::https://github.com/ruby-concurrency/$_gemname/archive/v$pkgver.tar.gz
	gemspecs.patch
	rakefile-remove-dock.patch
	rakefile-java-version.patch
	"
builddir="$srcdir/$_gemname-$pkgver"

# limited by jruby and openjdk
case "$CARCH" in x86_64 | aarch64)
	makedepends="$makedepends jruby java-jdk"
	subpackages="$subpackages $pkgname-java::noarch"

	export JRUBY_HOME="/usr/share/jruby"
esac

build() {
	rake repackage

	RUBYLIB="$(pwd)/lib" gem install --local \
		--install-dir dist-ext \
		--ignore-dependencies \
		--no-document \
		--verbose \
		pkg/$_gemname-ext-$pkgver.gem
}

check() {
	gem install --local \
		--install-dir .gems \
		--ignore-dependencies \
		--no-document \
		--verbose \
		pkg/$_gemname-*.gem

	GEM_PATH=".gems" rspec --tag ~unfinished --tag ~buggy --tag ~edge spec
}

package() {
	local gemdir="$pkgdir/$(ruby -e 'puts Gem.default_dir')"

	gem install --local \
		--install-dir "$gemdir" \
		--ignore-dependencies \
		--no-document \
		--verbose \
		pkg/$_gemname-$pkgver.gem

	# Remove unnecessary files.
	rm -r "$gemdir"/cache \
		"$gemdir"/build_info \
		"$gemdir"/doc
	cd "$gemdir"/gems/$_gemname-$pkgver
	rm -rf ext Gemfile Rakefile *.md
}

java() {
	pkgdesc="$pkgdesc (Java extension)"
	depends="$pkgname=$pkgver-r$pkgrel"
	install_if="$pkgname=$pkgver-r$pkgrel jruby"

	amove $(ruby -e 'puts Gem.default_dir')/gems/$_gemname-$pkgver/lib/$_gemname/*/*.jar
}

ext() {
	pkgdesc="$pkgdesc (C extension)"
	depends="$pkgname=$pkgver-r$pkgrel"

	local gemdir="$subpkgdir/$(ruby -e 'puts Gem.default_dir')"

	cd "$builddir"/dist-ext

	mkdir -p "$gemdir"
	cp -r extensions gems specifications "$gemdir"/

	# Remove unnecessary files and rubbish...
	find "$gemdir"/extensions/ -name mkmf.log -delete
	cd "$gemdir"/gems/$_gemname-ext-$pkgver
	rm -r ext/ lib/concurrent/*.so *.md
	rmdir -p --ignore-fail-on-non-empty "$(pwd)/lib/concurrent"
}

sha512sums="
d08a20ec465429875ddb1b1bcc7a75e74cfdb3113e4141e7d7f690ddd6fe50af978b7911bf0457a3e535fd79e463e423c34d23f77e645ecb6dd69704b17fdad6  ruby-concurrent-ruby-1.3.4.tar.gz
2db9dbd791f52647c7ba66b6537575ccbaa402de0316169cfc887f8f25093f0507e144eca86a4f1baaefc9cd268474460ed1ea09713b5ffb354642c036bc46f0  gemspecs.patch
964384305dbe7d0578e80117587235acd2ab8e812aaa9353a13157ad92574b6db632fe96b144950e64377f94af26fa2ec1bad1c829493c48d379c0c91e1282b7  rakefile-remove-dock.patch
f3bb000a843277719a687aebe6dd582aa590bd9456b3a067be4bed2efe5026bf8b794aea293dc359c721ece67e696885452a80daf6a80d69907df9098abd69a7  rakefile-java-version.patch
"
